package com.zhaoyue.study.mq.rabbitmq;

import com.rabbitmq.client.*;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 2017/2/21
 * Time: 17:55
 * To change this template use File | Settings | File Templates.
 */
public class Cdemo {

    public static final String QUEUE_NAME = "hello";

    public static void main(String[] args) throws Exception {

        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.56.56");
        factory.setUsername("test");
        factory.setPassword("123456");

        Connection connection = factory.newConnection();//连接
        Channel channel = connection.createChannel();//创建通道

        //
        Consumer consumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                String message = new String(body,"UTF-8");
                System.out.println("收到的消息：" + message);
            }
        };

        //参数为别为：队列名，是否自动ask，回调处理
        channel.basicConsume(QUEUE_NAME,true,consumer);
          //channel.close();
          //connection.close();
    }
}
